package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.FileService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.file.vo.*;
import com.ibigcat.module.mdm.dao.entity.FileDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.FileMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 文件 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FileServiceImpl implements FileService {

    @Resource
    private FileMapper fileMapper;

    @Override
    public Long createFile(FileSaveReqVO createReqVO) {
        // 插入
        FileDO file = BeanUtils.toBean(createReqVO, FileDO.class);
        fileMapper.insert(file);
        // 返回
        return file.getId();
    }

    @Override
    public void updateFile(FileSaveReqVO updateReqVO) {
        // 校验存在
        validateFileExists(updateReqVO.getId());
        // 更新
        FileDO updateObj = BeanUtils.toBean(updateReqVO, FileDO.class);
        fileMapper.updateById(updateObj);
    }

    @Override
    public void deleteFile(Long id) {
        // 校验存在
        validateFileExists(id);
        // 删除
        fileMapper.deleteById(id);
    }

    private void validateFileExists(Long id) {
        if (fileMapper.selectById(id) == null) {
            throw exception(FILE_NOT_EXISTS);
        }
    }

    @Override
    public FileDO getFile(Long id) {
        return fileMapper.selectById(id);
    }

    @Override
    public PageResult<FileDO> getFilePage(FilePageReqVO pageReqVO) {
        return fileMapper.selectPage(pageReqVO);
    }

}